package com.mz.ozy.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.mz.ozy.R;
import com.mz.ozy.bean.TransferBean;
import com.mz.ozy.widget.CornerTransform;

import java.util.List;


/**
 * Created by Administrator on 2018/11/28.
 * banner的适配器
 */

public class BannerPagerAdapter1 extends PagerAdapter {
    private final int type;
    private List<TransferBean.BalancemapBean> mList;
    private Context mContext;
    private int defaultImg = R.mipmap.default_avatar;//默认图片
    private int mRoundCorners = -1;
    private int mCurPosition = 0;

    public void setCurPosition(int curPosition) {
        mCurPosition = curPosition;
    }

    /**
     * 默认
     *
     * @param defaultImg
     */
    public void setDefaultImg(int defaultImg) {
        this.defaultImg = defaultImg;
    }

    /**
     * 设置圆角
     *
     * @param mRoundCorners
     */
    public void setmRoundCorners(int mRoundCorners) {
        this.mRoundCorners = mRoundCorners;
    }

    /**
     * 点击回调
     */
    public static interface OnClickImagesListener {
        void onImagesClick(int position);
    }

    private OnClickImagesListener mImagesListener;

    public void setOnClickImagesListener(OnClickImagesListener listener) {
        mImagesListener = listener;

    }

    public BannerPagerAdapter1(List<TransferBean.BalancemapBean> list, Context context, int type) {
        this.mList = list;
        this.mContext = context;
        this.type = type;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        final View rootView = (View) object;
        final Integer tag = (Integer) rootView.getTag();
        if (tag == mCurPosition) {
            TextView textView = rootView.findViewById(R.id.tv_banlece);
            if (!TextUtils.isEmpty(mList.get(mCurPosition).getBalance())) {
                textView.setText(mList.get(mCurPosition).getBalance());
            }
            return tag;
        } else {
            return super.getItemPosition(object);
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.banner_img_layout1, container, false);
        view.setTag(position);
        ImageView imageView = (ImageView) view.findViewById(R.id.img);
        TextView textView = (TextView) view.findViewById(R.id.tv_banlece);
        if (type == 0) {
            if (position == 0) {
                imageView.setImageResource(R.mipmap.kt_y);
            } else {
                imageView.setImageResource(R.mipmap.gq_y);
            }
        } else {
            if (position == 0) {
                imageView.setImageResource(R.mipmap.usdt_y);
            } else if (position == 1) {
                imageView.setImageResource(R.mipmap.kt_y);
            } else {
                imageView.setImageResource(R.mipmap.gq_y);
            }
        }
        final int index = position % mList.size();
        if (!TextUtils.isEmpty(mList.get(index).getBalance())) {
            textView.setText(mList.get(index).getBalance());
        }
        //OnClick
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImagesListener.onImagesClick(index);
            }
        });

        container.addView(view);
        return view;
    }

    /**
     * 加载图片
     */
    public void LoadImage(String url, ImageView imageview) {
        if (mRoundCorners == -1) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .dontAnimate()//防止设置placeholder导致第一次不显示网络图片,只显示默认图片的问题
                    .placeholder(defaultImg)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);   //在load()和into()之间，可以串连添加各种功能。
            Glide.with(mContext)
                    .load(url)
                    .apply(options)
                    .into(imageview);
        } else {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .dontAnimate()//防止设置placeholder导致第一次不显示网络图片,只显示默认图片的问题
                    .placeholder(defaultImg)
                    .transform(new CornerTransform(mContext, mRoundCorners))
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);   //在load()和into()之间，可以串连添加各种功能。
            Glide.with(mContext)
                    .load(url)
                    .apply(options)
                    .into(imageview);
        }
    }


}